home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / Shaders / TexturingAndModeling:AProceduralApproach / KMShaders / KMTerran.sl < prev    next >
Encoding:
Text File  |  1995-03-22  |  7.1 KB  |  226 lines

  1. /* modified by wave to KMTerran for name space protection... */
  2. /*
  3.  * terran.sl - surface for an Earth-like planet.
  4.  *
  5.  *
  6.  * DESCRIPTION:
  7.  *      When put on a sphere, sets the color to look like relatively
  8.  *   Earth-like.  The shader works by using a variety of fractal 
  9.  *   turbulence and mottling techniques.
  10.  *      Note that there is a companion displacement shader "terranbump"
  11.  *   which is necessary to get the best effect.  If you do this, it is
  12.  *   important that the parameters common to "terran" and "terranbump"
  13.  *   both be set to the same values.  Otherwise you get bumpy mountains
  14.  *   in the middle of the ocean.
  15.  *
  16.  *
  17.  * PARAMETERS:
  18.  *    Ka, Kd - the usual meaning
  19.  *    spectral_exp, lacunarity, octaves - control the fractal characteristics
  20.  *                of the bump pattern.
  21.  *    bump_scale - scaling of the mountains
  22.  *    multifractal - zero uses fBm noise, nonzero uses multifractal
  23.  *    dist_scale - scaling for multifractal distortion
  24.  *    offset - elevation offset
  25.  *    sea_level - obvious
  26.  *    mtn_scale - scaling factor for mountains
  27.  *    lat_scale, nonlinear, purt_scale, map_exp - control scaling of 
  28.  *               terrain type by latitude
  29.  *    ice_caps - latitude at which ice caps tend to form on the oceans
  30.  *    depth_scale, depth_max - scaling factor and max depth of oceans
  31.  *    mottle_limit, mottle_scale, moddle_dim, mottle_mag - control the
  32.  *               mottling that adds detail to lower latitude regions.
  33.  *
  34.  *
  35.  * HINTS:
  36.  *       The default values for the shader assume that the planet is
  37.  *    represented by a unit sphere.  The texture space and/or parameters
  38.  *    to this shader will need to be altered if the size of your planet
  39.  *    is radically different.
  40.  *       For best results, use with the "terranbump" displacement shader,
  41.  *    and add a cloud layer using either "planetclouds" or "venusclouds".
  42.  *
  43.  *
  44.  * AUTHOR: Ken Musgrave.
  45.  *    Conversion to Shading Language and minor modifications by Larry Gritz.
  46.  *
  47.  *
  48.  * REFERENCES:
  49.  *
  50.  *
  51.  * HISTORY:
  52.  *    ???? - original texture developed by F. Ken Musgrave.
  53.  *    Feb 1994 - Conversion to Shading Language by L. Gritz
  54.  *
  55.  * last modified 1 March 1994 by lg
  56.  */
  57.  
  58.  
  59. #ifdef BMRT
  60. #define snoise(x) (2*(noise(x)-0.5))
  61. #else
  62. /* This is because PRMAN's noise has less range than BMRT's */
  63. #define snoise(x) (2.5*(noise(x)-0.5))
  64. #endif
  65.  
  66. #define DNoise(x) ((2*(point noise(x))) - point(1,1,1))
  67. #define VLNoise(Pt,scale) (snoise(DNoise(Pt)+(scale*Pt)))
  68. #define N_OFFSET 0.7
  69. #define VERY_SMALL 0.0001
  70.  
  71.  
  72. surface
  73. KMTerran (float Ka = .5, Kd = .7;
  74.       float spectral_exp = 0.5;
  75.       float lacunarity = 2, octaves = 7;
  76.       float bump_scale = 0.07;
  77.       float multifractal = 0;
  78.       float dist_scale = .2;
  79.       float offset = 0;
  80.       float sea_level = 0;
  81.       float mtn_scale = 1;
  82.       float lat_scale = 0.95;
  83.       float nonlinear = 0;
  84.       float purt_scale = .9;
  85.       float map_exp = 0;
  86.       float ice_caps = 0.9;
  87.       float depth_scale = 1;
  88.       float depth_max = .5;
  89.       float mottle_limit = 0.75;
  90.       float mottle_scale = 20;
  91.       float mottle_dim = .25;
  92.       float mottle_mag = .02;)
  93. {
  94.   point PP;
  95.   point PtN;
  96.   float chaos, latitude, purt;
  97.   color Ct;
  98.   point Ptexture, tp;
  99.   float l, o, a, i, weight;      /* Loop variables for fBm calc */
  100.   float bumpy;
  101.  
  102.   /* Do all shading in shader space */
  103.   Ptexture = transform ("shader", P);
  104.   PtN = normalize (Ptexture);      /* Version of Ptexture with radius 1 */
  105.  
  106.   /**********************************************************************
  107.    * First, figure out where we are in relation to the oceans/mountains.
  108.    * Note: this section of code must be identical to "terranbump" if you
  109.    *       expect these two shaders to work well together.
  110.    **********************************************************************/
  111.  
  112.   if (multifractal == 0) {    /* use a "standard" fBm bump function */
  113.       o = 1;  l = 1;  bumpy = 0;
  114.       for (i = 0;  i < octaves;  i += 1) {
  115.       bumpy += o * snoise (l * Ptexture);
  116.       l *= lacunarity;
  117.       o *= spectral_exp;
  118.         }
  119.     }
  120.   else {            /* use a "multifractal" fBm bump function */
  121.       /* get "distortion" vector, as used with clouds */
  122.       Ptexture += dist_scale * DNoise (Ptexture);
  123.       /* compute bump vector using MfBm with displaced point */
  124.       o = spectral_exp;  tp = Ptexture;
  125.       weight = abs (VLNoise (tp, 1.5));
  126.       bumpy = weight * snoise (tp);
  127.       for (i = 1;  i < octaves  &&  weight >= VERY_SMALL;  i += 1) {
  128.       tp *= lacunarity;
  129.       /* get subsequent values, weighted by previous value */
  130.       weight *= o * (N_OFFSET + snoise(tp));
  131.       weight = clamp (abs(weight), 0, 1);
  132.       bumpy += snoise(tp) * min (weight, spectral_exp);
  133.       o *= spectral_exp;
  134.     }
  135.     }
  136.  
  137.   /* get the "height" of the bump, displacing by offset */
  138.   chaos = bumpy + offset;
  139.   /* set bump for land masses (i.e., areas above "sea level") */
  140.   if (chaos > sea_level) {
  141.       chaos *= mtn_scale;
  142. /*      sea_level *= mtn_scale; */
  143.     }
  144.  
  145.  
  146.   /************************************************************************
  147.    * Step 2: Assign a climite type, roughly by latitude.
  148.    ************************************************************************/
  149.  
  150.   /* make climate symmetric about equator -- use the "v" parameter */
  151.   latitude = abs (zcomp (PtN));
  152.  
  153.   /* fractally purturb color map offset using "chaos" */
  154.   /*  "nonlinear" scales purturbation-by-z */
  155.   /*  "purt_scale" scales overall purturbation */
  156.   latitude += chaos*(nonlinear*(1-latitude) + purt_scale);
  157.   if (map_exp > 0)
  158.        latitude = lat_scale * pow(latitude,map_exp);
  159.   else latitude *= lat_scale;
  160.  
  161.  
  162.   if (chaos > sea_level) {
  163.       /* Choose color of land based on the following spline.
  164.        * Ken originally had a huge table.  I was too lazy to type it in,
  165.        * so I used a scanned photo of the real Earth to select some
  166.        * suitable colors.  -- lg
  167.        */
  168. /*
  169.       Ct = spline (latitude,
  170.            color (.529, .412, .2745),
  171.            color (.529, .412, .2745),
  172.            color (.529, .412, .2745),
  173.            color (.255, .341,  0),
  174.            color (.256, .341, .141),
  175.            color (.235, .392, .235),
  176.            color (.490, .494, .1176),
  177.            color (.655, .529, .392),
  178.            color (.769, .616, .314),
  179.            color (.976, .820, .471),
  180.            color (1,1,1),
  181.            color (1,1,1));
  182. */
  183.       Ct = spline (latitude,
  184.            color (.5, .39, .2),
  185.            color (.5, .39, .2),
  186.            color (.5, .39, .2),
  187.            color (.2, .3,  0),
  188.            color (.085, .2, .04),
  189.            color (.065, .22, .04),
  190.            color (.5, .42, .28),
  191.            color (.6, .5, .23),
  192. /*           color (.976, .820, .471), */
  193.            color (1,1,1),
  194.            color (1,1,1));
  195.  
  196.      /* mottle the color some */
  197.      if (latitude < mottle_limit) {
  198.          PP = mottle_scale * Ptexture;
  199.      l = 1;  o = 1;
  200.      for (i = 0;  i < 6;  i += 1) {
  201.          purt += o * snoise (l * PP);
  202.          l *= 2;
  203.          o *= mottle_dim;
  204.        }
  205.      Ct += (mottle_mag * purt) * (color (0.5,0.175,0.5));
  206.        }
  207.     }
  208.   else { 
  209.       /* Oceans */
  210.       Ct = color(.1,.2,.5);
  211.       if (ice_caps > 0  &&  latitude > ice_caps)
  212.       Ct = color(1,1,1);  /* Ice color */
  213.       else {
  214.       /* Adjust color of water to darken deeper seas */
  215.           chaos -= sea_level;
  216.       chaos *= depth_scale;
  217.       chaos = max (chaos, -depth_max);
  218.       Ct *= (1+chaos);
  219.         }
  220.     }
  221.  
  222.   /* Shade using matte model */
  223.   Oi = 1;
  224.   Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(faceforward(normalize(N),I)));
  225. }
  226.